<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="逻辑删除" prop="isDelete">
        <el-input
          v-model.trim="queryParams.isDelete"
          placeholder="请输入逻辑删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="参数组件" prop="paramComponent">
        <el-input
          v-model.trim="queryParams.paramComponent"
          placeholder="请输入参数组件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="sql索引" prop="indexOfSql">
        <el-input
          v-model.trim="queryParams.indexOfSql"
          placeholder="请输入sql索引"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="打标组件" prop="tagComponent">
        <el-input
          v-model.trim="queryParams.tagComponent"
          placeholder="请输入打标组件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否生效" prop="isEffective">
        <el-input
          v-model.trim="queryParams.isEffective"
          placeholder="请输入是否生效"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="格式化组件" prop="formatComponent">
        <el-input
          v-model.trim="queryParams.formatComponent"
          placeholder="请输入格式化组件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="业务名称" prop="businessName">
        <el-input
          v-model.trim="queryParams.businessName"
          placeholder="请输入业务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['apiDefine:BiApiDefine:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['apiDefine:BiApiDefine:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['apiDefine:BiApiDefine:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['apiDefine:BiApiDefine:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleexecute"
          >执行</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="BiApiDefineList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <!-- <el-table-column label="逻辑删除" align="center" prop="isDelete" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="参数组件" align="center" prop="paramComponent"
        ><template v-slot="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="handlecomponentName(scope.row)"
            >{{ scope.row.paramComponent }}</span
          >
        </template></el-table-column
      >
      <el-table-column label="sql索引" align="center" prop="indexOfSql">
        <template v-slot="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="selectformisNew(scope.row)"
            >{{ scope.row.indexOfSql }}</span
          >
        </template></el-table-column
      >
      <el-table-column label="打标组件" align="center" prop="tagComponent" />
      <el-table-column label="是否生效" align="center" prop="isEffective">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEffective"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="格式化组件"
        align="center"
        prop="formatComponent"
      />
      <el-table-column label="业务名称" align="center" prop="businessName" />
      <el-table-column label="业务类型" align="center" prop="businessType" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['apiDefine:BiApiDefine:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['apiDefine:BiApiDefine:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="参数填写"
      :visible.sync="openexecute"
      width="600px"
      append-to-body
    >
      <el-form ref="dataForm" :model="dataForm" label-width="100px">
        <el-form-item label="数据来源" prop="salesGrade">
          <el-input
            v-model.trim="dataForm.salesGrade"
            placeholder="请输入数据来源"
          />
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker
            clearable
            v-model="dataForm.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker
            clearable
            v-model="dataForm.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 添加或修改API配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="逻辑删除" prop="isDelete">
          <el-input v-model.trim="form.isDelete" placeholder="请输入逻辑删除" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="参数组件" prop="paramComponent">
          <el-input
            v-model.trim="form.paramComponent"
            placeholder="请输入参数组件"
          />
        </el-form-item>
        <el-form-item label="sql索引" prop="indexOfSql">
          <el-input
            style="width: 200px"
            v-model="form.indexOfSql"
            placeholder="请输入sql索引"
          />
          <el-button @click="selectindexOfSql(form)" size="mini">
            Sql配置
          </el-button>
        </el-form-item>
        <!-- <el-form-item label="打标组件" prop="tagComponent">
          <el-input
            v-model.trim="form.tagComponent"
            placeholder="请输入打标组件"
          />
        </el-form-item>
        <el-form-item label="是否生效" prop="isEffective">
          <el-input
            v-model.trim="form.isEffective"
            placeholder="请输入是否生效"
          />
        </el-form-item> -->
        <el-form-item label="格式化组件" prop="formatComponent">
          <el-input
            v-model.trim="form.formatComponent"
            placeholder="请输入格式化组件"
          />
        </el-form-item>
        <el-form-item label="业务名称" prop="businessName">
          <el-input
            v-model.trim="form.businessName"
            placeholder="请输入业务名称"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- sql索引 -->
    <el-dialog
      :visible.sync="openindexOfSql"
      width="1500px"
      append-to-body
      v-if="openindexOfSql"
    >
      <div class="el-dialog-div">
        <component
          :is="'BiSqlFirstField'"
          :row="row"
          @handleSqlId="handleindexOfSql"
        ></component>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="openInfo"
      width="1500px"
      append-to-body
      v-if="openInfo"
    >
      <div class="el-dialog-div">
        <component
          :is="'BiSqlFirstField'"
          :row="row"
          @handleSqlId="handleSqlId"
        ></component>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="openConf"
      width="1500px"
      append-to-body
      v-if="openConf"
    >
      <div class="el-dialog-div">
        <component :is="'BiApiParamConf'" :rowInfo="rowInfo"></component>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="opentableData"
      width="1500px"
      append-to-body
      v-if="opentableData"
    >
      <el-table
        class="tb-edit"
        highlight-current-row
        :data="tableData"
        border
        style="width: 100%"
      >
        <template>
          <el-table-column
            v-for="(col, index) in cols"
            :key="index"
            :prop="cols[index].field_name"
            align="center"
            width="160"
            sortable
          >
            <template v-slot:header>
              <span>{{ cols[index].field_comment }} </span>
            </template>
            <template slot-scope="scope">
              <image-preview
                v-if="
                  String(scope.row[cols[index].field_name]).includes('profile')
                "
                :src="scope.row[cols[index].field_name]"
                :width="30"
                :height="30"
              />
              <span v-else>
                {{ scope.row[cols[index].field_name] }}
              </span>
            </template>
          </el-table-column>
        </template>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiApiDefine,
  getBiApiDefine,
  delBiApiDefine,
  addBiApiDefine,
  updateBiApiDefine,
  execute,
} from "@/api/apiDefine/BiApiDefine";
import BiSqlFirstField from "@/views/conf/BiSqlFirstField/index.vue";
import BiApiParamConf from "@/views/apiDefine/BiApiParamConf/index.vue";

export default {
  name: "BiApiDefine",
  components: {
    BiSqlFirstField,
    BiApiParamConf,
  },
  data() {
    return {
      opentableData: false,
      cols: [],
      tableData: [],

      dataForm: {
        salesGrade: "VFU,天猫",
      },
      openexecute: false,
      openConf: false,
      rowInfo: {},
      row: {},
      openindexOfSql: false,
      openInfo: false,
      inputValue: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // API配置表格数据
      BiApiDefineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isDelete: null,
        paramComponent: null,
        indexOfSql: null,
        tagComponent: null,
        isEffective: null,
        formatComponent: null,
        businessName: null,
        businessType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleexecute() {
      if (this.ids.length != 1) {
        this.$modal.msgError("请只选择一条数据");
        return;
      }
      this.openexecute = true;
    },
    onSubmit() {
      let obj = {
        data: this.dataForm,
        query: { id: this.ids[0] },
      };
      execute(obj).then((response) => {
        this.tableData = response.data.tableData ? response.data.tableData : [];
        // this.total = response.data.total ? response.data.total : 0;
        this.cols = [];
        this.cols = response.data.cols ? response.data.cols : [];
        this.$modal.msgSuccess("执行成功");
        this.openexecute = false;
        this.opentableData = true;
      });
    },
    /** 查询API配置列表 */
    getList() {
      this.loading = true;
      listBiApiDefine(this.queryParams).then((response) => {
        this.BiApiDefineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        isDelete: null,
        remark: null,
        paramComponent: null,
        indexOfSql: null,
        tagComponent: null,
        isEffective: null,
        formatComponent: null,
        businessName: null,
        businessType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加API配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiApiDefine(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改API配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiApiDefine(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiApiDefine(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除API配置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiApiDefine(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "apiDefine/BiApiDefine/export",
        {
          ...this.queryParams,
        },
        `BiApiDefine_${new Date().getTime()}.xlsx`
      );
    },
    selectindexOfSql(row) {
      this.row = { id: row.id, sqlId: row.indexOfSql };
      this.openindexOfSql = true;
    },
    //form里的
    handleindexOfSql(id) {
      this.form.indexOfSql = id;
      this.openindexOfSql = false;
    },
    //table里的
    handleSqlId(id) {
      delete this.rowInfo.indexOfSql;
      this.form = { ...this.rowInfo, indexOfSql: id };
      updateBiTableRelationshipConfig(this.form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.openInfo = false;
        this.getList();
      });
    },
    selectformisNew(row) {
      this.rowInfo = row;
      this.row = { id: row.id, sqlId: row.indexOfSql };
      this.openInfo = true;
    },
    handlecomponentName(row) {
      this.rowInfo = row;
      this.openConf = true;
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.isEffective === "0" ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '"生效吗？')
        .then(function () {
          return updateBiApiDefine(row);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isEffective = row.isEffective === "0" ? "1" : "0";
        });
    },
  },
};
</script>
